﻿using System;
using HelixSoftware.Azure.MethodExecutionServices;

namespace HelixSoftware.Azure.Data
{
    public abstract class DataObjectProxy<T> : IDataObjectService<T> where T : DataObject
    {
        private IDataObjectService<T> _Service;

        protected DataObjectProxy(IDataObjectService<T> service)
        {
            _Service = service;
        }

        public virtual T CreateNew()
        {
            var proxy = MethodExecutionProxyFactory.GetProxy();
            return proxy.Execute<T>(_Service.CreateNew);
        }

        public object Save(T obj)
        {
            if (obj.SaveAction != SaveAction.None)
            {
                var proxy = MethodExecutionProxyFactory.GetProxy();
                return proxy.Execute<T, Object>(_Service.Save, obj);
            }
            else
            {
                return null;
            }
        }
    }
}
